package com.deep.common;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import org.kohsuke.args4j.spi.StringArrayOptionHandler;

import java.util.List;


@ToString
@Getter
@Setter
public class ArgsConfig {
    @Option(name = "-r", aliases = {"--recid"}, metaVar = "type of int", usage = "整型类型参数")
    public Integer recid = 1145140;

    @Option(name = "-i", aliases = {"--int"}, metaVar = "type of int", usage = "整型类型参数")
    public Integer parallelism = 1;

    @Option(name = "-p", aliases = {"--profile"}, metaVar = "type of string", usage = "配置")
    public String profile = "dev";


    @Option(name = "-l", aliases = {"--lists"}, metaVar = "type of lists", usage = "列表元素", handler = StringArrayOptionHandler.class)
    public List<String> lists;

    @Option(name = "-v", aliases = {"--version"}, metaVar = "show version", usage = "输出产品版本并退出", handler = BooleanOptionHandler.class)
    public boolean version = false;

    @Option(name = "-h", aliases = {"-?", "--help"}, metaVar = "show help info", usage = "帮助信息", handler = BooleanOptionHandler.class, help = true)
    public boolean help = false;

    @Option(name = "-k", aliases = {"--key"}, metaVar = "encrypt/decrypt key", usage = "加解密密钥")
    public String key;

    @Option(name = "-e", aliases = {"--encrypt"}, metaVar = "to encrypt", usage = "加密", depends = {"-k", "--key"}, forbids = {"-d", "--decrypt"})
    public boolean encrypt = false;

    @Option(name = "-d", aliases = {"--decrypt"}, metaVar = "to decrypt", usage = "解密", depends = {"-k", "--key"}, forbids = {"-e", "--encrypt"})
    public boolean decrypt = false;

    public static void main(String[] args) throws CmdLineException {
        ArgsConfig argsConfig = new ArgsConfig();
        CmdLineParser parser = new CmdLineParser(argsConfig);
        parser.setUsageWidth(ArgsConstant.USAGE_WIDTH);
        parser.parseArgument(args);
        // 没有参数
        if (args.length == 0) {
            argsConfig.showHelp(parser);
        }
        // 如果启动参数中包含 帮助参数项， 则打印参数信息
        if (argsConfig.isHelp()) {
            argsConfig.showHelp(parser);
        }
        System.out.println(argsConfig.toString());
    }

    public void showHelp(CmdLineParser parser) {
        System.out.println("参数说明 [options ...] [arguments...]");
        parser.printUsage(System.out);
    }

}
